//
//  CasesOnShowViewController.m
//  customer
//
//  Created by Holothurian on 2018/10/29.
//  Copyright © 2018年 yinlong. All rights reserved.
//

#import "CasesOnShowViewController.h"
#import "CasesShowInEffectViewController.h"
#import "CasesShowCollectionViewCell.h"
#import "CasesShowModel.h"

static NSString *ItemIdentifier = @"ItemIdentifier";

@interface CasesOnShowViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>{
    UICollectionViewFlowLayout *_flowLayout;
}

@property (nonatomic,strong)UICollectionView *collectionView;

@property (nonatomic,assign)NSInteger page;

@property (nonatomic,strong)NSMutableArray *dataSource;

@end

@implementation CasesOnShowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"案例展示";
    [self layoutCollectionView];
    [self loadData];
}

- (void)loadData{
    [self showLoading];
    [[QYNetManager sharedManager]POST:kCasesEffect parameters:@{@"page":[NSNumber numberWithInteger:self.page]} andApiTyp:CustomerApiTypGetOrders SuccessBlock:^(id responseObject) {
        [self hideLoading];
        NSLog(@"%@",responseObject[@"data"]);
        for (NSDictionary *dict in responseObject[@"data"]) {
            CasesShowModel *model = [CasesShowModel yy_modelWithDictionary:dict];
            [self.dataSource addObject:model];
        }
        [self endRefresh];
        [self.collectionView reloadData];
    
    } Failure:^(NSError *error) {
        [self hideLoading];
        [self endRefresh];
        [self showErrorTipWithTitle:CustomerPoolNetStr];
    }];
}


- (void)layoutCollectionView{
    
    _flowLayout = [[UICollectionViewFlowLayout alloc]init];
    _flowLayout.itemSize = CGSizeMake(F_DEVICE_W - 30 , F_DEVICE_H / 4.0);
    _flowLayout.minimumLineSpacing = 20;
    _flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, F_DEVICE_W, F_DEVICE_H - NAVIGATION_BAR_HEIGHT - bottomPadding(0)) collectionViewLayout: _flowLayout];
    collectionView.bounces = NO;
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:collectionView];
    self.collectionView = collectionView;
    [self.collectionView registerClass:[CasesShowCollectionViewCell class] forCellWithReuseIdentifier:ItemIdentifier];
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self loadData];
    }];
    self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.page++;
        [self loadData];
    }];
    
}

- (void)endRefresh{
    if (self.page == 1) {
        [self.collectionView.mj_header endRefreshing];
    }else{
        [self.collectionView.mj_footer endRefreshing];
    }
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [self.dataSource count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CasesShowCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ItemIdentifier forIndexPath:indexPath];
    CasesShowModel *model = self.dataSource[indexPath.row];
    [cell cellForLayout:model];
    return cell;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(20, 0, 20, 0);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    CasesShowInEffectViewController *effect = [CasesShowInEffectViewController new];
    CasesShowModel *mode = self.dataSource[indexPath.row];
    effect.effect_id = mode.effect_id;
    TAB_PUSH(self, effect, YES);
}

- (NSMutableArray *)dataSource{
    if(!_dataSource){
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
